#!/usr/bin/env xonsh
from os.path import basename,dirname

dirpath = "/root/zst"
sqlite_db = dirpath+"/sqlite3.db"
rm -rf @(sqlite_db)
def main():
  for i in $(find @(dirpath)/*.csv.zst).split():
    print(i)
    csvfile = i[:-4]
    zstdcat @(i) >  @(csvfile)
    table = basename(csvfile)[:-4]
    cmd = '(echo .mode csv;echo .import %s %s)'%(csvfile,table)
    sh -c @(cmd) | sqlite3 @(sqlite_db)
    rm -rf @(csvfile)
main()
zstd -f --rm -19 @(sqlite_db)
